package com.qm.finger.protocol;


import com.qm.finger.FingerFun;
import com.qm.model.ReadData;
import com.qm.utils.ByteUtil;

public class FingerProtocol {
    public static final int Resp_NoAnswer = -1;
    public static final int Resp_Invalid = -2;

    private static byte[] BuildProtocol(byte head, byte cmd, byte[] dat, int dataLen, byte[] fix, int fixLen) {
        if (dat == null || dataLen < 0 || dataLen > dat.length)
            return new byte[0];
        byte[] ret = new byte[dataLen + 5];
        ret[0] = head;
        ret[1] = dat[0];
        ret[2] = cmd;
        System.arraycopy(fix, 0, ret, 3, fixLen);
        ret[ret.length - 1] = ByteUtil.getBCC(ret, ret.length - 1);
        return ret;
    }

    private static byte[] BuildSetProtocol(byte head, byte cmd, byte[] dat, int dataLen) {
        if (dat == null || dataLen < 0 || dataLen > dat.length)
            return new byte[0];
        byte[] ret = new byte[dataLen + 5];
        ret[0] = head;
        ret[1] = 0x00;
        ret[2] = cmd;
        ret[3] = dat[0];
        ret[4] = 0x00;
        ret[ret.length - 1] = ByteUtil.getBCC(ret, ret.length - 1);
        return ret;
    }


    public static boolean ParseXORProtocol(byte[] dat, int len, byte cmd) {
        if (dat == null || len < 4 || dat.length < len)
            return false;
        if (dat[0] != FingerFun.FINGER_HEAD.getCode())
            return false;
        if (ByteUtil.getXor(dat, len) != 0)
            return false;
        return dat[2] == cmd;
    }

    public static int ParseBBCProtocol(byte[] dat) {
        int len = dat.length;
        if (dat == null || len < 8)
            return Resp_NoAnswer;
        if (dat[0] != FingerFun.FINGER_HEAD.getCode())
            return Resp_NoAnswer;
        if (ByteUtil.getBCC(dat, len) != 0)
            return Resp_NoAnswer;
        return dat[4];
    }

    public static ReadData ParseReadProtocol(byte[] dat) {
        int len = dat.length;
        if (dat == null || len < 8)
            return null;
        if (dat[0] != FingerFun.FINGER_HEAD.getCode())
            return null;
        if (ByteUtil.getBCC(dat, len) != 0)
            return null;
        ReadData readData = new ReadData();
        readData.setCmd(dat[1]);
        byte[] bt = new byte[2];
        System.arraycopy(dat, 2, bt, 0, bt.length);
        readData.setUserID(ByteUtil.byteArrayToInt(bt));
        readData.setRet(dat[4]);
        return readData;
    }

    public static byte[] ParseReadFeatureProtocol(byte[] dat) {
        //  先检验数据  总长是固定的
        int len = dat.length;
        if (dat == null || len < 207)
            return null;
        byte[] headBT = new byte[8];
        byte[] dataBT = new byte[199];
        System.arraycopy(dat, 0, headBT, 0, headBT.length);
        System.arraycopy(dat, 8, dataBT, 0, dataBT.length);
        if (headBT[0] != FingerFun.FINGER_HEAD.getCode())
            return null;
        if (ByteUtil.getBCCFinger(headBT, 6) != 0)
            return null;
        if (dataBT[0] != FingerFun.FINGER_HEAD.getCode())
            return null;
        if (ByteUtil.getBCCFinger(dataBT, 197) != 0)
            return null;
        byte[] fingerData = new byte[193];
        System.arraycopy(dataBT, 4, fingerData, 0, fingerData.length);
        ;
        return fingerData;
    }

    /**
     * 光学指纹仪 封装的 协议指令  注：发送的均为8个字节
     **/
    //修改模块序列号
    public static byte[] BuildModifySerialNum(int num_1, int num_2, int num_3) {
        byte[] ret = new byte[8];
        ret[0] = FingerFun.FINGER_HEAD.getCode();
        ret[1] = FingerFun.MODIFY_SERIAL_NUM.getCode();
        ret[2] = (byte) (num_1 % 256);
        ret[3] = (byte) (num_2 % 256);
        ret[4] = (byte) (num_3 % 256);
        ret[5] = 0x00;
        ret[6] = ByteUtil.getBCC(ret);
        ret[7] = FingerFun.FINGER_HEAD.getCode();
        return ret;
    }

    //取消內部模块序列号
    public static byte[] BuildCancelSerialNum() {
        byte[] ret = new byte[8];
        ret[0] = FingerFun.FINGER_HEAD.getCode();
        ret[1] = FingerFun.CANCEL_SERIAL_NUM.getCode();
        ret[2] = 0x00;
        ret[3] = 0x00;
        ret[4] = 0x00;
        ret[5] = 0x00;
        ret[6] = ByteUtil.getBCC(ret);
        ret[7] = FingerFun.FINGER_HEAD.getCode();
        return ret;
    }

    //让内部模块处于休眠状态
    public static byte[] BuildDormant() {
        byte[] ret = new byte[8];
        ret[0] = FingerFun.FINGER_HEAD.getCode();
        ret[1] = FingerFun.DORMANT.getCode();
        ret[2] = 0x00;
        ret[3] = 0x00;
        ret[4] = 0x00;
        ret[5] = 0x00;
        ret[6] = ByteUtil.getBCC(ret);
        ret[7] = FingerFun.FINGER_HEAD.getCode();
        return ret;
    }

    //设置/读取指纹添加模式
    //model  0:允许重复  1：禁止重复
    //isRead 0:设置新的添加模式  1：读取当前的添加模式
    public static byte[] BuildSetReadModel(int model, int isRead) {
        byte[] ret = new byte[8];
        ret[0] = FingerFun.FINGER_HEAD.getCode();
        ret[1] = FingerFun.SET_READ_MODEL.getCode();
        ret[2] = 0x00;
        ret[3] = (byte) (model % 256);
        ret[4] = (byte) (isRead % 256);
        ret[5] = 0x00;
        ret[6] = ByteUtil.getBCC(ret);
        ret[7] = FingerFun.FINGER_HEAD.getCode();
        return ret;
    }

    //添加指纹-1
    public static byte[] BuildAddFinger_1(int userID) {
        byte[] ret = new byte[8];
        ret[0] = FingerFun.FINGER_HEAD.getCode();
        ret[1] = FingerFun.ADD_FINGER_FIRST.getCode();
        byte[] userIDBt = ByteUtil.intToBytes(userID);
        System.arraycopy(userIDBt, 0, ret, 2, userIDBt.length);
        ret[4] = (byte) (1 % 256);
        ret[5] = 0x00;
        ret[6] = ByteUtil.getBCC(ret);
        ret[7] = FingerFun.FINGER_HEAD.getCode();
        return ret;
    }

    //添加指纹-2
    public static byte[] BuildAddFinger_2(int userID) {
        byte[] ret = new byte[8];
        ret[0] = FingerFun.FINGER_HEAD.getCode();
        ret[1] = FingerFun.ADD_FINGER_SECOND.getCode();
        byte[] userIDBt = ByteUtil.intToBytes(userID);
        System.arraycopy(userIDBt, 0, ret, 2, userIDBt.length);
        ret[4] = (byte) (1 % 256);
        ret[5] = 0x00;
        ret[6] = ByteUtil.getBCC(ret);
        ret[7] = FingerFun.FINGER_HEAD.getCode();
        return ret;
    }

    //添加指纹-3
    public static byte[] BuildAddFinger_3(int userID) {
        byte[] ret = new byte[8];
        ret[0] = FingerFun.FINGER_HEAD.getCode();
        ret[1] = FingerFun.ADD_FINGER_THIRD.getCode();
        byte[] userIDBt = ByteUtil.intToBytes(userID);
        System.arraycopy(userIDBt, 0, ret, 2, userIDBt.length);
        ret[4] = (byte) (1 % 256);
        ret[5] = 0x00;
        ret[6] = ByteUtil.getBCC(ret);
        ret[7] = FingerFun.FINGER_HEAD.getCode();
        return ret;
    }

    //添加用户并将特征值上传
    public static byte[] BuildAddReadFinger(int userID, int userRole) {
        byte[] ret = new byte[8];
        ret[0] = FingerFun.FINGER_HEAD.getCode();
        ret[1] = FingerFun.ADD_UP_FINGER.getCode();
        byte[] userIDBt = ByteUtil.intToBytes(userID);
        System.arraycopy(userIDBt, 0, ret, 2, userIDBt.length);
        ret[4] = (byte) (userRole % 256);
        ret[5] = 0x00;
        ret[6] = ByteUtil.getBCC(ret);
        ret[7] = FingerFun.FINGER_HEAD.getCode();
        return ret;
    }

    //删除指定用户
    public static byte[] BuildDeleteUserByID(int userID) {
        byte[] ret = new byte[8];
        ret[0] = FingerFun.FINGER_HEAD.getCode();
        ret[1] = FingerFun.DELETE_USER_BY_ID.getCode();
        byte[] userIDBt = ByteUtil.intToBytes(userID);
        System.arraycopy(userIDBt, 0, ret, 2, userIDBt.length);
        ret[4] = 0x00;
        ret[5] = 0x00;
        ret[6] = ByteUtil.getBCC(ret);
        ret[7] = FingerFun.FINGER_HEAD.getCode();
        return ret;
    }

    //删除所有用户
    // userRole 0:刪除所有用戶 1、2、3：刪除权限为1、2、3的所有用户
    public static byte[] BuildDeleteAllUser(int userRole) {
        byte[] ret = new byte[8];
        ret[0] = FingerFun.FINGER_HEAD.getCode();
        ret[1] = FingerFun.DELETE_ALL_USER.getCode();
        ret[2] = 0x00;
        ret[3] = 0x00;
        ret[4] = (byte) (userRole % 256);
        ret[5] = 0x00;
        ret[6] = ByteUtil.getBCC(ret);
        ret[7] = FingerFun.FINGER_HEAD.getCode();
        return ret;
    }

    //获取用户总数
    public static byte[] BuildGetUserNum() {
        byte[] ret = new byte[8];
        ret[0] = FingerFun.FINGER_HEAD.getCode();
        ret[1] = FingerFun.GET_USER_NUM.getCode();
        ret[2] = 0x00;
        ret[3] = 0x00;
        ret[4] = 0x00;
        ret[5] = 0x00;
        ret[6] = ByteUtil.getBCC(ret);
        ret[7] = FingerFun.FINGER_HEAD.getCode();
        return ret;
    }

    //1:1比对指纹信息
    public static byte[] BuildCompare1_1(int userID) {
        byte[] ret = new byte[8];
        ret[0] = FingerFun.FINGER_HEAD.getCode();
        ret[1] = FingerFun.COMPARE1_1.getCode();
        byte[] userIDBt = ByteUtil.intToBytes(userID);
        System.arraycopy(userIDBt, 0, ret, 2, userIDBt.length);
        ret[4] = 0x00;
        ret[5] = 0x00;
        ret[6] = ByteUtil.getBCC(ret);
        ret[7] = FingerFun.FINGER_HEAD.getCode();
        return ret;
    }

    //1:N比对指纹信息
    public static byte[] BuildCompare1_N() {
        byte[] ret = new byte[8];
        ret[0] = FingerFun.FINGER_HEAD.getCode();
        ret[1] = FingerFun.COMPARE1_N.getCode();
        ret[2] = 0x00;
        ret[3] = 0x00;
        ret[4] = 0x00;
        ret[5] = 0x00;
        ret[6] = ByteUtil.getBCC(ret);
        ret[7] = FingerFun.FINGER_HEAD.getCode();
        return ret;
    }

    //获取用户权限
    public static byte[] BuildGetUserRole(int userID) {
        byte[] ret = new byte[8];
        ret[0] = FingerFun.FINGER_HEAD.getCode();
        ret[1] = FingerFun.GET_USER_ROLE.getCode();
        byte[] userIDBt = ByteUtil.intToBytes(userID);
        System.arraycopy(userIDBt, 0, ret, 2, userIDBt.length);
        ret[4] = 0x00;
        ret[5] = 0x00;
        ret[6] = ByteUtil.getBCC(ret);
        ret[7] = FingerFun.FINGER_HEAD.getCode();
        return ret;
    }

    //采集图像并提取特征值上传
    public static byte[] BuildCollectIMGExtractFeature() {
        byte[] ret = new byte[8];
        ret[0] = FingerFun.FINGER_HEAD.getCode();
        ret[1] = FingerFun.COLLECT_IMAGE_EXTRACT_FEATURE.getCode();
        ret[2] = 0x00;
        ret[3] = 0x00;
        ret[4] = 0x00;
        ret[5] = 0x00;
        ret[6] = ByteUtil.getBCC(ret);
        ret[7] = FingerFun.FINGER_HEAD.getCode();
        return ret;
    }

    //采集图像


    //把采集的指纹数据写入指纹模块  数据包
    private static byte[] BuildRegisterUserData(int userID, byte[] fingerData) {
        byte[] ret = new byte[6 + fingerData.length];
        ret[0] = FingerFun.FINGER_HEAD.getCode();
        byte[] userIDBt = ByteUtil.intToBytes(userID);
        System.arraycopy(userIDBt, 0, ret, 1, userIDBt.length);
        ret[3] = 0x01;
        System.arraycopy(fingerData, 0, ret, 4, fingerData.length);
        ret[ret.length - 2] = ByteUtil.getBCCFinger(ret, fingerData.length + 3);
        ret[ret.length - 1] = FingerFun.FINGER_HEAD.getCode();
        return ret;
    }

    //把采集的指纹数据写入指纹模块  数据头部
    private static byte[] BuildRegisterUserHead(int len) {
        byte[] ret = new byte[8];
        ret[0] = FingerFun.FINGER_HEAD.getCode();
        ret[1] = FingerFun.REGISTER_FINGER.getCode();
        byte[] userIDBt = ByteUtil.intToBytes(len);
        System.arraycopy(userIDBt, 0, ret, 2, userIDBt.length);
        ret[4] = 0x00;
        ret[5] = 0x00;
        ret[ret.length - 2] = ByteUtil.getBCCFinger(ret, 5);
        ret[ret.length - 1] = FingerFun.FINGER_HEAD.getCode();
        return ret;
    }

    //采集数据
    public static byte[] BuildRegisterUser(int userID, byte[] fingerData) {
        byte[] dataBT = BuildRegisterUserData(userID, fingerData);
        byte[] headBT = BuildRegisterUserHead(fingerData.length + 3);
        byte[] data = new byte[dataBT.length + headBT.length];
        System.arraycopy(headBT, 0, data, 0, headBT.length);
        System.arraycopy(dataBT, 0, data, headBT.length, dataBT.length);
        return data;
    }
}
